//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  http://www.nopCommerce.com/License.aspx. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations under the License.
// 
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
// 
// Contributor(s): _______. 
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using NopSolutions.NopCommerce.BusinessLogic.Audit;
using NopSolutions.NopCommerce.BusinessLogic.Caching;
using NopSolutions.NopCommerce.BusinessLogic.Configuration.Settings;
using NopSolutions.NopCommerce.BusinessLogic.CustomerManagement;
using NopSolutions.NopCommerce.BusinessLogic.Data;
using NopSolutions.NopCommerce.BusinessLogic.Directory;
using NopSolutions.NopCommerce.BusinessLogic.Infrastructure;
using NopSolutions.NopCommerce.BusinessLogic.Orders;
using NopSolutions.NopCommerce.BusinessLogic.Products.Attributes;
using NopSolutions.NopCommerce.BusinessLogic.Promo.Discounts;
using NopSolutions.NopCommerce.BusinessLogic.Tax;
using NopSolutions.NopCommerce.Common;
using NopSolutions.NopCommerce.Common.Utils;

namespace NopSolutions.NopCommerce.BusinessLogic.Alesko
{
    /// <summary>
    /// Banner service
    /// </summary>
    public partial class BannerService : IBannerService
    {
        #region Constants

        private const string DELIVERIES_BY_ID_KEY = "Nop.Banner.id-{0}";
        private const string DELIVERIES_PATTERN_KEY = "Nop.Banner.";

        #endregion

        #region Fields

        /// <summary>
        /// Object context
        /// </summary>
        private readonly NopObjectContext _context;

        /// <summary>
        /// Cache manager
        /// </summary>
        private readonly ICacheManager _cacheManager;

        #endregion

        #region Ctor

        /// <summary>
        /// Ctor
        /// </summary>
        /// <param name="context">Object context</param>
        public BannerService(NopObjectContext context)
        {
            this._context = context;
            this._cacheManager = new NopRequestCache();
        }

        #endregion

        #region Methods
        /// <summary>
        /// Deletes a banner mapping
        /// </summary>
        /// <param name="bannerId">Banner mapping identifier</param>
        public void DeleteBanner(int bannerId)
        {
            var banner = GetBannerById(bannerId);
            if (banner == null)
                return;


            if (!_context.IsAttached(banner))
                _context.Banners.Attach(banner);
            _context.DeleteObject(banner);
            _context.SaveChanges();
        }

        /// <summary>
        /// Gets a banner mapping
        /// </summary>
        /// <param name="bannerId">Banner mapping identifier</param>
        /// <returns>Banner mapping</returns>
        public Banner GetBannerById(int bannerId)
        {
            if (bannerId == 0)
                return null;


            var query = from pp in _context.Banners
                        where pp.BannerID == bannerId
                        select pp;
            var banner = query.SingleOrDefault();
            return banner;
        }

        /// <summary>
        /// Gets all banners
        /// </summary>
        /// <returns>Banner collection</returns>
        public List<Banner> GetAllBanners()
        {
            var query = from pp in _context.Banners
                        orderby pp.BannerType, pp.DisplayOrder
                        select pp;

            var banners = query.ToList();
            return banners;
        }

        /// <summary>
        /// Gets all banners
        /// </summary>
        /// <returns>Banner collection</returns>
        public List<Banner> GetAllBannersByType(Banner.BannerTypeEnum bannerType)
        {
            var query = from pp in _context.Banners
                        where pp.BannerType == (int)bannerType
                        orderby pp.DisplayOrder
                        select pp;

            var banners = query.ToList();
            return banners;
        }

        /// <summary>
        /// Inserts a banner mapping
        /// </summary>
        /// <param name="banner">Banner mapping</param>
        public void InsertBanner(Banner banner)
        {
            if (banner == null)
                throw new ArgumentNullException("banner");
            
            _context.Banners.AddObject(banner);
            _context.SaveChanges();
        }

        /// <summary>
        /// Updates the banner mapping
        /// </summary>
        /// <param name="banner">Banner mapping</param>
        public void UpdateBanner(Banner banner)
        {
            if (banner == null)
                throw new ArgumentNullException("banner");


            if (!_context.IsAttached(banner))
                _context.Banners.Attach(banner);

            _context.SaveChanges();
        }

        #endregion

        #region Properties
        
        /// <summary>
        /// Gets a value indicating whether cache is enabled
        /// </summary>
        public bool CacheEnabled
        {
            get
            {
                return IoC.Resolve<ISettingManager>().GetSettingValueBoolean("Cache.BannerManager.CacheEnabled");
            }
        }
        #endregion
    }
}
